package com.ruoyi.redtea.dualprevent.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.redtea.dualprevent.domain.bo.TrainUserInfoBo;
import com.ruoyi.redtea.dualprevent.domain.entity.TrainDept;
import com.ruoyi.redtea.dualprevent.domain.entity.TrainDeptUser;
import com.ruoyi.redtea.dualprevent.domain.entity.TrainUser;
import com.ruoyi.redtea.dualprevent.domain.vo.TrainUserInfoVo;
import com.ruoyi.redtea.dualprevent.mapper.TrainDeptMapper;
import com.ruoyi.redtea.dualprevent.mapper.TrainDeptUserMapper;
import com.ruoyi.redtea.dualprevent.mapper.TrainUserMapper;
import com.ruoyi.redtea.dualprevent.service.TrainUserInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author makejava
 * @since 2024-05-14 09:39:47
 */
@Service("trainUserService")
public class TrainUserInfoServiceImpl implements TrainUserInfoService {
    @Resource
    private TrainUserMapper trainUserMapper;

    @Resource
    private TrainDeptMapper trainDeptMapper;

    @Resource
    private TrainDeptUserMapper trainDeptUserMapper;

    @Override
    public boolean insertUser(TrainUser trainUser) {
        return trainUserMapper.insert(trainUser)==1;
    }

    @Override
    public boolean updateUser(TrainUser trainUser) {
        return trainUserMapper.updateById(trainUser)==1;
    }

    @Override
    public boolean insertDept(TrainDept trainDept) {
        return trainDeptMapper.insert(trainDept)==1;
    }

    @Override
    public boolean updateDept(TrainDept trainDept) {
        return trainDeptMapper.updateById(trainDept)==1;
    }

    @Override
    public boolean insertDeptUser(TrainDeptUser trainDeptUser) {
        return trainDeptUserMapper.insert(trainDeptUser)==1;
    }

    @Override
    public boolean updateDeptUser(TrainDeptUser trainDeptUser) {
        return trainDeptUserMapper.updateById(trainDeptUser)==1;
    }

    @Override
    public List<TrainUserInfoVo> selectList() {
        List<TrainUserInfoVo> trainUserInfoVo = trainUserMapper.selectAll();
        return trainUserInfoVo;
    }

    @Override
    public TrainUserInfoVo queryUserInfoById(Long id) {
        TrainUserInfoVo trainUserInfoVo = trainUserMapper.selectUserInfoVoById(id);
        return trainUserInfoVo;
    }

    @Override
    public boolean insertUserInfo(TrainUserInfoBo trainUserInfoBo) {
        TrainUser trainUser = BeanUtil.toBean(trainUserInfoBo, TrainUser.class);
        boolean saveUser = this.insertUser(trainUser);

        Integer deptId = trainUserInfoBo.getDeptId();
        TrainDeptUser trainDeptUser = new TrainDeptUser();
        trainDeptUser.setDeptId(deptId);
        trainDeptUser.setSonType(2);

        boolean saveUpdateDeptUser = insertDeptUser(trainDeptUser);
        if(!saveUser||!saveUpdateDeptUser){
            throw new ServiceException("添加用户失败！");
        }else {
            return true;
        }
    }

    @Override
    public boolean updateUserInfo(TrainUserInfoBo trainUserInfoBo) {
        return false;
    }
}

